﻿#include "qmotherboardinfoprovider.h"
#include <QProcess>
#include <QFile>
#include <QTextStream>
#include <QDebug>

#ifdef Q_OS_WIN
#include <windows.h>
#endif

QMotherboardInfoProvider::QMotherboardInfoProvider(QObject *parent)
    : QObject(parent) {}

QList<QMotherboardInfoProvider::MotherboardDetails> QMotherboardInfoProvider::getAllMotherboardDetails() const {
    QList<MotherboardDetails> details;

#ifdef Q_OS_WIN
    details = getMotherboardDetailsWindows();
#elif defined(Q_OS_LINUX)
    details = getMotherboardDetailsLinux();
#else
    qWarning() << "Unsupported platform for motherboard information.";
#endif

    return details;
}

QList<QMotherboardInfoProvider::MotherboardDetails> QMotherboardInfoProvider::getMotherboardDetailsWindows() const {
    QList<MotherboardDetails> details;

    QProcess process;
    process.start("powershell.exe", QStringList() << "-Command" << "Get-WmiObject Win32_BaseBoard | Select-Object Manufacturer, Product, SerialNumber, Version, Caption | Format-List");
    process.waitForFinished();

    QString output = QString::fromLocal8Bit(process.readAllStandardOutput());
    QStringList lines = output.split("\n");

    MotherboardDetails info;
    for (const QString &line : lines) {
        if (line.contains("Manufacturer")) {
            info.manufacturer = line.split(":").last().trimmed();
        } else if (line.contains("Product")) {
            info.product = line.split(":").last().trimmed();
        } else if (line.contains("SerialNumber")) {
            info.serialNumber = line.split(":").last().trimmed();
        } else if (line.contains("Version")) {
            info.version = line.split(":").last().trimmed();
        } else if (line.contains("Caption")) {
            info.caption = line.split(":").last().trimmed();
        }
    }

    if (!info.manufacturer.isEmpty() || !info.product.isEmpty()) {
        details.append(info);
    }

    return details;
}

QList<QMotherboardInfoProvider::MotherboardDetails> QMotherboardInfoProvider::getMotherboardDetailsLinux() const {
    QList<MotherboardDetails> details;

    QStringList files = {"manufacturer", "product_name", "serial_number", "version", "board_name"};
    MotherboardDetails info;

    for (const QString &file : files) {
        QFile f("/sys/class/dmi/id/" + file);
        if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&f);
            QString value = in.readLine().trimmed();
            if (file == "manufacturer") {
                info.manufacturer = value;
            } else if (file == "product_name") {
                info.product = value;
            } else if (file == "serial_number") {
                info.serialNumber = value;
            } else if (file == "version") {
                info.version = value;
            } else if (file == "board_name") {
                info.caption = value;
            }
            f.close();
        }
    }

    if (!info.manufacturer.isEmpty() || !info.product.isEmpty()) {
        details.append(info);
    }

    return details;
}
